import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, FormControl, Validators, ValidatorFn, AbstractControl } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { NzNotificationService } from 'ng-zorro-antd';

import { AccountService, User } from '../../../DM/account.service';
@Component({
  selector: 'app-personal-center',
  templateUrl: './personal-center.component.html',
  styleUrls: ['./personal-center.component.css']
})
export class PersonalCenterComponent implements OnInit {

  _user: User = new User;

  _passwordVaildForm: FormGroup;
  _usernameValidForm: FormGroup;

  constructor(
    private accountService: AccountService,
    private fb: FormBuilder,
    private msgService: NzMessageService,
    private noticeService: NzNotificationService
  ) {
    this._passwordVaildForm = this.fb.group({
      currentPassword: ['', [Validators.required]],
      newPassword: ['', [Validators.required]],
      repeatNewPassword: ['', [Validators.required, this._repeatPwdValidtor()]],
    });

    this._usernameValidForm = this.fb.group({
      username: ['', [Validators.required]]
    });
  }

  async ngOnInit() {
    this._user = await this.accountService.getCurrentUserInfo();
    // console.log(this._user);
  }

  // formsubmit for change password
  async _submitForm_pwd() {
    try {
      for (const i in this._passwordVaildForm.controls) {
        this._passwordVaildForm.controls[i].markAsDirty();
      }
      if (this._passwordVaildForm.valid) {
        await this.accountService.changePassword(this._passwordVaildForm.get('currentPassword').value, this._passwordVaildForm.get('newPassword').value);
        this.msgService.success('更新成功');
        this._passwordVaildForm.reset();
        this.noticeService.blank('密码变更', '您刚刚变更了您的密码');
      }
    } catch (err) {
      this.msgService.error(err);
      this.noticeService.blank('密码变更', `由于${err}，您未成功变更密码`);
    }
  }

  // formsubmit for change username
  async _submitForm_username() {
    try {
      for (const i in this._usernameValidForm.controls) {
        this._usernameValidForm.controls[i].markAsDirty();
      }
      if (this._usernameValidForm.valid) {
        await this.accountService.update(this._usernameValidForm.get('username').value);
        this.msgService.success('更新成功');
        this._usernameValidForm.reset();
        this.noticeService.blank('用户名变更', '您刚刚变更了您的用户名');
      }
    } catch (err) {
      this.msgService.error(err);
      this.noticeService.blank('用户名变更', `由于${err}，您未成功变更用户名`);
    }
  }

  // getformcontrol
  _getPwdFormControl(key: string) {
    return this._passwordVaildForm.controls[key];
  }
  _getUsrNameFormControl(key: string) {
    return this._usernameValidForm.controls[key];
  }

  // repeatpwdvalidtor
  _repeatPwdValidtor(): ValidatorFn {
    return (control: AbstractControl): { [key: string]: any } => {
      if (!this._passwordVaildForm) return { 'repeat': true };
      return control.value == this._passwordVaildForm.get('newPassword').value ? null : { 'repeat': true };
    }
  }

}
